package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.nineoldandroids.view.ViewHelper;

/**
 * Created by dahai2070 on 2018/5/14 0014.
 */

public class ScaleView extends FrameLayout {

    private ScaleGestureDetector scaleGestureDetector;
    private long lastMultiTouchTime;
    private boolean needToHandle;

    public ScaleView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ScaleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ScaleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean isScaleEvent=false;
        int pointerCount = ev.getPointerCount();
        if (pointerCount>1){
            isScaleEvent=true;
        }

        return isScaleEvent;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int pointerCount = event.getPointerCount(); // 获得多少点
        if (pointerCount > 1) {// 多点触控，
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    needToHandle = true;
                    break;
                case MotionEvent.ACTION_MOVE:

                    break;
                case MotionEvent.ACTION_POINTER_2_UP://第二个手指抬起的时候
                    needToHandle = true;
                    break;

                default:
                    break;
            }
            return scaleGestureDetector.onTouchEvent(event);//让mScaleGestureDetector处理触摸事件
        } else {
            return true;
        }
    }

    private class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {

            float previousSpan = detector.getPreviousSpan();// 前一次双指间距
            float currentSpan = detector.getCurrentSpan();// 本次双指间距
            float scaleFactor = detector.getScaleFactor();


//            if (currentSpan < previousSpan) {
//                // 缩小
//                scale = preScale - (previousSpan - currentSpan) / 1000;
//            } else {
//                // 放大
//                scale = preScale + (currentSpan - previousSpan) / 1000;
//            }
//            // 缩放view
//            if (scale > 0.5) {
//                if (myListener != null) {
//                   myListener.onScale(scale);
//                }
//                ViewHelper.setScaleX(MyScrollView.this, scale);// x方向上缩放
//                ViewHelper.setScaleY(MyScrollView.this, scale);// y方向上缩放

            // matrix.postScale(scale, scale, detector.getFocusX(), detector.getFocusY());


//            }
            View childAt = getChildAt(0);
            ViewHelper.setScaleX(childAt, scaleFactor);// x方向上缩放
            ViewHelper.setScaleY(childAt, scaleFactor);// y方向上缩放
//            ViewHelper.setPivotX(childAt,0f);
//            ViewHelper.setPivotY(childAt,0f);

            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {

            lastMultiTouchTime = System.currentTimeMillis();
        }
    }

}
